//
//  ZHYYTable.m
//  ZHYYView
//
//  Created by yuan zhang on 2021/9/15.
//  Copyright © 2021 yuan zhang. All rights reserved.
//

#import "ZHYYTable.h"
#import "TableModel.h"
@interface ZHYYTable()<NSTableViewDelegate,NSTableViewDataSource>
@end


@implementation ZHYYTable
{
    NSTableView *_tableView;
    NSInteger _columnNumber;
    NSScrollView * _scrollView;

}


- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    // Drawing code here.
}

-(id)initWithFrame:(NSRect)frameRect ColumnNumber:(NSInteger)columnNumer{
    self =[super initWithFrame:frameRect];
    if (self) {
        [self createTableWithColumnNumber:columnNumer];
        _columnNumber = columnNumer;
    }
    return self;
}



-(void)tableModelData{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"plist"];
        if (filePath) {
            self.tbModel = [TableModel dataList:filePath];
            NSLog(@"table data: %@", self.tbModel);
            NSLog(@"table count:%ld",self.tbModel.count);
            [_tableView reloadData];
    }
}


//创建表格，设置默认属性
-(void)createTableWithColumnNumber:(NSInteger)columnNumber{
    _scrollView    = [[NSScrollView alloc] init];
    _scrollView.hasVerticalScroller  = YES;
    _scrollView.frame = self.bounds;
    [self addSubview:_scrollView];
    _tableView = [[NSTableView alloc]initWithFrame:self.bounds];
//    _tableView.usesAlternatingRowBackgroundColors =YES; //是否开启斑马线
//    _tableView.gridStyleMask=NSTableViewSolidVerticalGridLineMask;//竖直分割线
    _tableView.gridStyleMask=NSTableViewDashedHorizontalGridLineMask; //设置分割线样式
    for (int i =0; i<columnNumber;i++ ) {
        NSTableColumn *column =[self createColumnWithIdentifier:[NSString stringWithFormat:@"column%d",i+1] Title:[NSString stringWithFormat:@"column%d",i+1]];
        column.width = self.frame.size.width / columnNumber; //设置Column长度，默认等分
        [_tableView addTableColumn:column];
    }
    NSLog(@"%@",[_tableView tableColumns]);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //创建数据
    [self tableModelData];
    
    _scrollView.contentView.documentView = _tableView;
}



//创建column，设置默认属性
-(NSTableColumn*)createColumnWithIdentifier:(NSString*)identifier Title:(NSString*)title{
    NSTableColumn * column = [[NSTableColumn alloc]initWithIdentifier:identifier];
    column.width=100;//设置列宽度
    column.title=title;
   
    column.headerCell.backgroundColor=[NSColor systemBlueColor];
    column.headerCell.font=[NSFont systemFontOfSize:14];
    column.headerCell.alignment=NSTextAlignmentCenter;
    return column;
    
}

/*
//通过数组设置表格数据，数组中嵌套每列需要的数组数据，可以为空
-(void)setTableDataWithArray:(NSMutableArray*)data {
    for (int i =0; i<_columnNumber; i++) {
        NSString *key =[NSString stringWithFormat:@"column%d",i+1];
        if (i<self.tbModel.count) {
            [_data setValue:data[i] forKey:key];
        }
    }
}

//通过dict设置表格数据，dict key要和column.identifiery一致
-(void)setTableDataWithDict:(NSMutableDictionary*)data {
    for (int i =0; i<_columnNumber; i++) {
        NSString *key =[NSString stringWithFormat:@"column%d",i+1];
        [_data setValue:[data objectForKey:key] forKey:key];
    }
}
*/


//设置行数
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return self.tbModel.count;
}

-(NSUInteger)tableRowCount{
    return self.tbModel.count;
}

//设置表格显示数据
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    TableModel *tb =[self.tbModel objectAtIndex:row];
    if ([tableColumn.identifier isEqualToString:@"column1"]) {
        return tb.ID;
    }else if ([tableColumn.identifier isEqualToString:@"column2"]) {
        return tb.name;
    } else if ([tableColumn.identifier isEqualToString:@"column3"]) {
        return tb.DUT1;
    }else if ([tableColumn.identifier isEqualToString:@"column4"]) {
        return tb.DUT2;
    }else{
        return @"";
    }
}
 


//设置cell状态
-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSTextFieldCell *acell = cell;
    acell.alignment =NSTextAlignmentCenter;
//    acell.textColor=[NSColor systemGreenColor];
//    if ([acell.stringValue containsString:@"PASS"]) {
//        [acell setTextColor:[NSColor greenColor]];
//    } else if ([acell.stringValue containsString:@"FAIL"]) {
//        [acell setTextColor:[NSColor redColor]];
//    } else if ([acell.stringValue containsString:@"TESTING"]) {
//        [acell setTextColor:[NSColor systemYellowColor]];
//    } else {
//        [acell setTextColor:[NSColor blueColor]];
//    }
}


//cell是否可以编辑
-(BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    return  NO;
}


//鼠标悬停在cell上时 显示的提示文案
-(NSString*)tableView:(NSTableView *)tableView toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation
{
    return [NSString stringWithFormat:@"column:%@,row:%ld",tableColumn.identifier,row+1];
}




//设置行高
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    CGFloat rowhight =24;
    return rowhight;
}

/*
添加一行时会调用的回调
*/
- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row{
    NSLog(@"add row");
}


/*
移除一行时会调用的回调
*/
- (void)tableView:(NSTableView *)tableView didRemoveRowView:(NSTableRowView *)rowView forRow:(NSInteger)row{
     NSLog(@"remove row");
}


/*
当cell的宽度不够显示完全cell的内容时，设置是否允许鼠标放置扩展cell
*/
- (BOOL)tableView:(NSTableView *)tableView shouldShowCellExpansionForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row{
    return YES;
}


/*
设置是否加强cell的交互能力，这样一些按钮状态的修改也会触发cell编辑的状态
*/
- (BOOL)tableView:(NSTableView *)tableView shouldTrackCell:(NSCell *)cell forTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row{
    return NO;
}

//==================================================================================================

//重新加载数据
-(void)reloadDataTB{
//    NSLog(@"reload all Data");
    [_tableView reloadData];
}


//重新加载指定位置的数据
-(void)reloadDataWithRowIndex:(NSIndexSet *)rowIndex columnIndex:(NSIndexSet *)columnIndex{
    [_tableView reloadDataForRowIndexes:rowIndex columnIndexes:columnIndex];
   
   
}

-(void)testScroll:(NSInteger)row{
     [_tableView scrollRowToVisible:row];
}

//更改表格数据
-(void)changeDUT1Data:(NSString*)data atRowIndex:(NSInteger)row {
   TableModel* item = [_tbModel objectAtIndex:row];
    item.DUT1 = data;
}

-(void)changeDUT2Data:(NSString*)data atRowIndex:(NSInteger)row {
   TableModel* item = [_tbModel objectAtIndex:row];
    item.DUT2 = data;
}

@end
